package com.atguigu.jxc.service.impl;



import com.atguigu.jxc.dao.ReturnListGoodsMapper;
import com.atguigu.jxc.dao.ReturnListGoodsMapper;
import com.atguigu.jxc.dao.ReturnListMapper;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jxc
 * @description:
 * @author: ZJH
 * @create: 2023-02-11 19:45
 **/
@Service
@SuppressWarnings("all")
public class ReturnListServiceImpl implements ReturnListService {

    @Autowired
    private ReturnListMapper returnListMapper;
    @Autowired
    private ReturnListGoodsMapper returnListGoodsMapper;

    @Override
    public List<ReturnList> ShowReturnList(String returnNumber, Integer supplierId, Integer state, String sTime,String eTime) {
        List<ReturnList> returnList = returnListMapper.findReturnList(returnNumber, supplierId, state, sTime, eTime);
        return returnList;
    }

    @Override
    public List<ReturnListGoods> showReturnListGoods(Integer returnListId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("return_list_goods_id",returnListId);
        List<ReturnListGoods> returnListGoods = returnListGoodsMapper.selectList(queryWrapper);
        return returnListGoods;
    }

    @Transactional
    @Override
    public ServiceVO deleteReturnListGoods(Integer returnListId) {
        int flag = 0;
        //删除退货单列表的值
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("return_list_id",returnListId);
        flag += returnListGoodsMapper.delete(queryWrapper);
        //删除退货单单值
        flag += returnListMapper.deleteById(returnListId);

        ServiceVO serviceVO;
        if(flag!=0){
            serviceVO = new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        }else{
            serviceVO = new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS,null);
        }
        return serviceVO;
    }

    @Override
    public List<Map<String, Object>> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        List<ReturnList> returnListList = returnListMapper.getReturnList(sTime, eTime);
        for (ReturnList returnList : returnListList) {
            Integer returnListId = returnList.getReturnListId();
            List<ReturnListGoods> listGoodsList = returnListGoodsMapper.getReturnGoods(returnListId, goodsTypeId, codeOrName);
            for (ReturnListGoods returnListGoods : listGoodsList) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("number", returnList.getReturnNumber());
                map.put("date", returnList.getReturnDate());
                map.put("supplierName", returnList.getSupplierName());
                map.put("goodsType", returnListGoods.getGoodsTypeName());
                map.put("unit", returnListGoods.getGoodsUnit());
                map.put("price", returnListGoods.getPrice());
                map.put("code", returnListGoods.getGoodsCode());
                map.put("name", returnListGoods.getGoodsName());
                map.put("num", returnListGoods.getGoodsNum());
                map.put("total", returnListGoods.getTotal());
                list.add(map);
            }
        }
        return list;
    }

}

